/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.CustomerMgtBackend.db;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Thomas
 */
public class DB_Access implements DB_Config {

    private static DB_Access theInstance = null;
    private DB_ConnectionPool conPool;
    private final static String userNameQuery = "SELECT username FROM customer WHERE username = ?;";
    private final static String registerStat = "INSERT INTO customer (username, password) VALUES(?, ?);";
    private final static String userNamePasswQuery = "SELECT username, password FROM customer WHERE username = ? AND password = ?;";

    private DB_Access() throws ClassNotFoundException, SQLException {
        conPool = DB_ConnectionPool.getInstance();
    }

    public boolean checkUser(String username) throws Exception {
        Connection con = conPool.getConnection();
        PreparedStatement pStat = con.prepareStatement(userNameQuery);
        pStat.setString(1, username);
        ResultSet rs = pStat.executeQuery();
        boolean found = rs.next();
        conPool.releaseConnection(con);

        return found;
    }

    public boolean checkPassword(String username, String password) throws Exception{
        Connection con = conPool.getConnection();
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(password.getBytes());
        byte[] hashCode = md.digest();

        String convertToHexString = convertToHexString(hashCode);
        PreparedStatement preStat = con.prepareStatement(userNamePasswQuery);
        preStat.setString(1, username);
        preStat.setString(2, convertToHexString);
        ResultSet rs = preStat.executeQuery();
        boolean loginOK = false;
        
        if(rs.next()){
            if(rs.getString("username").equals(username) && rs.getString("password").equals(convertToHexString)){
                loginOK = true;
            }
        }
        
        conPool.releaseConnection(con);
        return loginOK;
    }
    
    public boolean regists(String user, String password) throws NoSuchAlgorithmException, SQLException, Exception {
        Connection con = conPool.getConnection();
        //Encrypt passwort
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(password.getBytes());
        byte[] hashCode = md.digest();

        String convertToHexString = convertToHexString(hashCode);
        System.out.println(convertToHexString);
        PreparedStatement preStat = con.prepareStatement(registerStat);
        preStat.setString(1, user);
        preStat.setString(2, convertToHexString);
        int lines = preStat.executeUpdate();
        conPool.releaseConnection(con);
        return lines == 1 ? true : false;
    }

    public String convertToHexString(byte[] hashCode) {
        String ret = "";
        for (byte b : hashCode) {
            int value = (b & 0x7F) + (b < 0 ? 128 : 0);
            ret += (value < 16 ? "0" : "");
            ret += Integer.toHexString(value).toUpperCase();
        }
        return ret;
    }

    public static DB_Access getInstance() throws ClassNotFoundException, SQLException {
        if (theInstance == null) {
            theInstance = new DB_Access();
        }
        return theInstance;
    }
}
